package listnode;

public class Merge {
    public ListNode Merge (ListNode pHead1, ListNode pHead2) {
        // write code here
        ListNode ret = new ListNode(-1001);
        ListNode cur = ret;
        while(pHead1 != null && pHead2 != null) {
            if(pHead1.val <= pHead2.val){
                ret.next = pHead1;
                pHead1 = pHead1.next;
                ret = ret.next;
            }else {
                ret.next = pHead2;
                ret = ret.next;
                pHead2 = pHead2.next;
            }
        }
        if(pHead1 == null && pHead2 != null) {
            while(pHead2 != null) {
                ret.next = pHead2;
                ret = ret.next;
                pHead2 = pHead2.next;
            }
        }
        if(pHead1 != null &&pHead2 == null) {
            while(pHead1 != null) {
                ret.next = pHead1;
                ret = ret.next;
                pHead1 = pHead1.next;
            }
        }


        return cur.next;
    }
}
